using System;
using Csla;
using ENT.GP.Datos.MysqlHelper;
using System.Data;

namespace ENT.GP.Core.Rol
{
    [Serializable]
    public class Rol : BusinessBase<Rol>
    {
        #region Business Methods
              
        private static PropertyInfo<int> IdRolProperty = RegisterProperty<int>(o => o.IdRol, "Name");
        public int IdRol
        {
            get { return GetProperty(IdRolProperty); }
            set { SetProperty(IdRolProperty, value); }
        }

        private static PropertyInfo<string> NombreProperty = RegisterProperty<string>(o => o.Nombre, "Name");
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        private static PropertyInfo<string> DescripcionProperty = RegisterProperty<string>(o => o.Descripcion, "Name");
        public string Descripcion
        {
            get { return GetProperty(DescripcionProperty); }
            set { SetProperty(DescripcionProperty, value); }
        }

        private static PropertyInfo<string> CodigoProperty = RegisterProperty<string>(o => o.Codigo, "Name");
        public string Codigo
        {
            get { return GetProperty(CodigoProperty); }
            set { SetProperty(CodigoProperty, value); }
        }

        //Nuevo Id Empresa
        private static PropertyInfo<int> IDEmpresaProperty = RegisterProperty<int>(o => o.IDEmpresa, "Name");
        public int IDEmpresa
        {
            get { return GetProperty(IDEmpresaProperty); }
            set { SetProperty(IDEmpresaProperty, value); }
        }



        public static bool VerificarRol(string sRol,int idEmp)
        {
            return ExisteRolCommand.Execute(sRol, idEmp);
        }
        public static bool VerificarCodigo(string sCodigo, int idEmp)
        {
            return ExisteCodigoCommand.Execute(sCodigo, idEmp);
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(RuleMethod, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("Name", "Role");
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowEdit(typeof(Usuario), "Role");
        }

        #endregion

        #region Factory Methods

        public static Rol NewRol()
        {
            return DataPortal.Create<Rol>();
        }

        public static Rol GetRol(int id)
        {
            return DataPortal.Fetch<Rol>(
              new SingleCriteria<Rol, int>(id));
        }

        public static void DeleteRol(int id)
        {
            DataPortal.Delete(new SingleCriteria<Rol, int>(id));
        }

        private Rol()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(SingleCriteria<Rol, int> criteria)
        {
            DataSet dsDatos = new DataSet();
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[2];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDU", criteria.Value);
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("OPC", 1);
            dsDatos = objhm.EjecutarStoreProcedure(parametros, "Obtener_Rol");
            if (dsDatos.Tables[0].Rows.Count > 0)
            {

                LoadProperty(IdRolProperty, Convert.ToInt32(dsDatos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(NombreProperty, dsDatos.Tables[0].Rows[0][1].ToString());
                LoadProperty(DescripcionProperty, dsDatos.Tables[0].Rows[0][2].ToString());
                LoadProperty(CodigoProperty, dsDatos.Tables[0].Rows[0][3].ToString());
              
               
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[4];
            try
            {

                parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("NOMBRE_ROL", Nombre);
                parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("DESCRIPCION_ROL", Descripcion);
                parametros[2] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", Codigo);
                parametros[3] = new MySql.Data.MySqlClient.MySqlParameter("IDEMPR1", IDEmpresa);
                objhm.EjecutarStoreProcedure("Insertar_Rol", parametros);
               
            }
            catch (Exception)
            {

                throw;
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[4];
            try
            {
               
                parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDR", IdRol);
                parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("NOMBRER", Nombre);
                parametros[2] = new MySql.Data.MySqlClient.MySqlParameter("DESCRIP", Descripcion);
                parametros[3] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", Codigo);
             
              
                objhm.EjecutarStoreProcedure("Actualizar_Rol", parametros);
            }
            catch (Exception)
            {
                throw;
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new SingleCriteria<Rol, int>(this.IdRol));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<Rol, int> criteria)
        {
            // TODO: delete values
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[1];

            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("IDR", IdRol);
            objhm.EjecutarStoreProcedure("Eliminar_Rol", parametros);

        }

        #endregion
    }

    [Serializable]
    public class ExisteRolCommand : CommandBase
    {
        private string _rol;
        private int _idEmp;
        #region Authorization Methods

        public static bool CanExecuteCommand()
        {
            // TODO: customize to check user role
            //return Csla.ApplicationContext.User.IsInRole("Role");
            return true;
        }

        #endregion

        #region Factory Methods

        public static bool Execute(string rol, int idEmp)
        {
            if (!CanExecuteCommand())
                throw new System.Security.SecurityException("Not authorized to execute command");

            ExisteRolCommand cmd = new ExisteRolCommand(rol, idEmp);
            cmd.BeforeServer();
            cmd = DataPortal.Execute<ExisteRolCommand>(cmd);
            cmd.AfterServer();
            return cmd.Result;
        }

        private ExisteRolCommand(string rol, int idEmp)
        { 
            /* require use of factory methods */
            _rol = rol;
            _result = false;
            _idEmp = idEmp;
        }

        #endregion

        #region Client-side Code

        // TODO: add your own fields and properties
        bool _result;

        public bool Result
        {
            get { return _result; }
        }

        private void BeforeServer()
        {
            // TODO: implement code to run on client
            // before server is called
        }

        private void AfterServer()
        {
            // TODO: implement code to run on client
            // after server is called
        }

        #endregion

        #region Server-side Code
               
        protected override void DataPortal_Execute()
        {
            int numeroUsuario = 0;
            object valret;
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[2];
            try
            {
              
                parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("rol", _rol);
                parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("IDEMP1", _idEmp);
                valret = objhm.EjecutarStoreProcedure("Comprobar_Rol", parametros, 1);
             
                int.TryParse(valret.ToString(),out numeroUsuario);
                if (numeroUsuario == 0)
                    _result = false;
                else
                
                    _result = true;
                 
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }

    [Serializable]
    public class ExisteCodigoCommand : CommandBase
    {
        private string _codigo;
        private int _idEmp;
        #region Authorization Methods

        public static bool CanExecuteCommand()
        {
            // TODO: customize to check user role
            //return Csla.ApplicationContext.User.IsInRole("Role");
            return true;
        }

        #endregion

        #region Factory Methods

        public static bool Execute(string codigo,int idEmp)
        {
            if (!CanExecuteCommand())
                throw new System.Security.SecurityException("Not authorized to execute command");

            ExisteCodigoCommand cmd = new ExisteCodigoCommand(codigo, idEmp);
            cmd.BeforeServer();
            cmd = DataPortal.Execute<ExisteCodigoCommand>(cmd);
            cmd.AfterServer();
            return cmd.Result;
        }

        private ExisteCodigoCommand(string codigo,int idEmp)
        {
            /* require use of factory methods */
            _codigo = codigo;
            _result = false;
            _idEmp = idEmp;
        }

        #endregion

        #region Client-side Code

        // TODO: add your own fields and properties
        bool _result;

        public bool Result
        {
            get { return _result; }
        }

        private void BeforeServer()
        {
            // TODO: implement code to run on client
            // before server is called
        }

        private void AfterServer()
        {
            // TODO: implement code to run on client
            // after server is called
        }

        #endregion

        #region Server-side Code

        protected override void DataPortal_Execute()
        {
            int numeroUsuario = 0;
            object valret;
            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySql.Data.MySqlClient.MySqlParameter[] parametros = new MySql.Data.MySqlClient.MySqlParameter[2];
            try
            {

                parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("codigo", _codigo);
                parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("IDEMP1", _idEmp);
                valret = objhm.EjecutarStoreProcedure("Comprobar_Codigo_Rol", parametros, 1);
                int.TryParse(valret.ToString(), out numeroUsuario);
                if (numeroUsuario == 0)
                    _result = false;
                else

                    _result = true;

            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion
    }

}
